package com.handinfo.android.uicontrols.controls;


import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.game.Role;
import com.handinfo.android.utils.Tools;

/**
 * 动画容器
 * @author handinfo
 */
public class DWAnimationBox extends DWControl
{

	public Actor m_actor = null;
	private int m_action = 0;
	
	public DWAnimationBox() {
		setShowWideHigh(60, 130);
	}

	public DWAnimationBox(Actor actor)
	{
		setAnimation(actor, 0, 0);
	}

	public DWAnimationBox(Actor actor, int x, int y)
	{
		setAnimation(actor, x, y);
	}

	public void setNearAnchor(DWControl control, int ownAnchor, int targetAnchor, int interspaceX, int interspaceY)
	{
		super.setNearAnchor(control, ownAnchor, targetAnchor, interspaceX, interspaceY);
		if (m_actor != null) {
			m_actor.m_posX = m_show_x + m_show_w / 2;
			m_actor.m_posY = m_show_y + m_show_h;
		}
	}

	private void setAnimation(Actor actor, int x, int y)
	{
		if (actor != null) {
			m_actor = new Actor();
			m_actor.m_anims = actor.m_anims;
			m_actor.m_anim = actor.m_anim;
			m_actor.m_resHead = actor.m_resHead;
			setPoint(x, y);
			m_actor.m_posX = m_show_x + m_show_w / 2;
			m_actor.m_posY = m_show_y + m_show_h;
			setShowWideHigh(60, 130);
		}
	}
	
	public void setAnimation(Actor actor) {
		if (m_actor != null) {
//			m_actor.destroy();
			m_actor = null;
		}
		if (actor != null) {
			if (actor instanceof Role) {
				m_actor = new Role();
			} else {
				m_actor = new Actor();
			}
			m_actor.m_anims = actor.m_anims;
			m_actor.m_anim = actor.m_anim;
			m_actor.m_resHead = actor.m_resHead;
			m_actor.m_posX = m_show_x + m_show_w / 2;
			m_actor.m_posY = m_show_y + m_show_h;
		}
	}
	
//	public void setPoint(int x, int y)
//	{
//		super.setPoint(x, y);
//		if (m_actor != null) {
//			m_actor.m_posX = m_show_x + 30;
//			m_actor.m_posY = m_show_y + 120;
//		}
//	}
	
	public void refreshControl()
	{
	}

	public void setAction(int action)
	{
		m_action = action;
		if (m_actor != null && m_actor.m_anims != null && m_actor.m_anim != null) {
			m_actor.setAction(m_action, true, true);
		}
	}

	public void render(DWGraphics g)
	{
		if (m_actor != null && m_actor.m_anims != null && m_actor.m_anim != null) {
			m_actor.m_posX = m_show_x + m_show_w / 2;
			m_actor.m_posY = m_show_y + m_show_h;
			try {
				m_actor.draw(g, 0, 0);
			} catch (Exception e) {
				Tools.debugPrintln("animation box actor exception!");
			}
			setClip(g);
			renderRect(g);
		}
	}
	
	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (m_actor != null && m_actor.m_anims != null && m_actor.m_anim != null) {
			m_actor.m_posX = x + m_show_x - offsetx + m_show_w / 2;
			m_actor.m_posY = y + m_show_y - offsety + m_show_h;
			try {
				m_actor.draw(g, 0, 0);
			} catch (Exception e) {
				Tools.debugPrintln("animation box actor exception!");
			}
			setSubClip(g, x, y, w, h, offsetx, offsety);
			renderLimitsRect(g, x, y, offsetx, offsety);
		}
	}

	public void logic()
	{
		try {
			if (m_actor != null && m_actor.m_anims != null && m_actor.m_anim != null)
				m_actor.nextFrame();
		} catch (Exception e) {
			Tools.debugPrintln("actor nextframe exception : " + m_actor.m_resHead.m_url);
		}
	}

	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			if (m_listener != null) {
				m_listener.OnClick();
				return true;
			}
		}
		return false;
	}

	public void destroy()
	{
		
	}
}
